/**
 * @file    base_math.c
 * @brief   基本算法库
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-26
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-26 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "base_math.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  计算最大值与最小值
 * @param  obj
 * @param  size
 * @param  data_buf
 */
void math_calc_volt_max(max_min_type *obj , uint8_t size ,uint16_t const *data_buf)
{
    uint8_t i = 0;
    max_min_type data_obj = {0};
    data_obj.min_sn = 1;
    data_obj.min_val = data_buf[0];
    data_obj.max_sn = 1;
    data_obj.max_val = data_buf[0];

    for( i = 1; i < size; ++ i)
    {
        if(data_obj.min_val > data_buf[i])
        {
            data_obj.min_sn = i + 1;
            data_obj.min_val = data_buf[i];
        }

        if(data_obj.max_val > data_buf[i])
        {
            data_obj.max_sn = i + 1;
            data_obj.max_val = data_buf[i];
        }
    }

    *obj = data_obj;
}

/**
 * @brief  冒泡排序算法
 * @param  src_buff : 数据缓冲区
 * @param  data_size : 数据长短
 * @param  out_buf : 有序数据列表
 */
void bubble_sort(uint16_t *src_buf ,uint8_t size, idx_data_type *out_buf) 
{
    uint8_t i = size - 1;
    uint8_t j = 0;
    uint8_t flag = 0;
    idx_data_type sw_temp = {0};

    for(i = 0;i < size; ++ i)
    { 
        out_buf[i].idx = i;
        out_buf[i].data = src_buf[i];
    }

    for(i = 0; i < size - 1; ++ i)
    {
        flag = 0; 
        for (j = 0; j < size - 1 - i; j ++) 
        {
            if(out_buf[j].data < out_buf[j+1].data)
            {
                sw_temp = out_buf[j];
                out_buf[j] = out_buf[j + 1];
                out_buf[j + 1] = sw_temp;
                flag = 1;
            }
        }
        
        if(0 == flag)
        {
            break;
        }
    }
}

uint8_t math_cmp_str(const uint8_t str1[],const uint8_t str2[] ,uint16_t len)
{
	uint16_t i = 0;
	for(i = 0;i < len ; ++ i)
	{
		if(str1[i] != str2[i])
			return FALSE;
	}
	
	return TRUE;
}

/**
 * @brief  获取二进制1的个数
 * @param  data : 数据
 * @param  data_num : 1的个数
 */
uint8_t math_get_data_bit1_num(uint32_t data)
{
	uint8_t data_num = 0;
	uint8_t i = 0;
	
	for( i = 0;i < 32; ++ i)
	{
		if(data & 0x01)
		{
			data_num ++ ;
		}
		
		data >>= 1;
		
		if(data == 0)
			break;
	}
	
	return data_num ;
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


